﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GXYDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // 在窗口或控件中
            var viewModel = new LoadPortViewModel();
            this.DataContext = viewModel;

            // 更新状态
            viewModel.UpdateSlotStatus(25, SlotStatus.Occupied);
            viewModel.UpdateSlotStatus(24, SlotStatus.Occupied);
            viewModel.UpdateSlotStatus(23, SlotStatus.Selected);
        }
    }

    public class SlotItem : INotifyPropertyChanged
    {
        private int _number;
        private SlotStatus _status;

        public int Number
        {
            get => _number;
            set
            {
                _number = value;
                OnPropertyChanged();
            }
        }

        public SlotStatus Status
        {
            get => _status;
            set
            {
                _status = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public enum SlotStatus
    {
        Empty,      // 空
        Occupied,   // 有晶圆
        Selected    // 选中
    }

    public class StatusToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is SlotStatus status)
            {
                switch (status)
                {
                    case SlotStatus.Occupied:
                        return new SolidColorBrush(Color.FromRgb(255, 215, 0)); // #FFD700
                    case SlotStatus.Selected:
                        return new SolidColorBrush(Color.FromRgb(255, 165, 0)); // 选中状态的颜色
                    default:
                        return Brushes.Transparent;
                }
            }
            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class NumberToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int number && number == 25)
                return Visibility.Visible;
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class LoadPortViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<SlotItem> _slots;
        public ObservableCollection<SlotItem> Slots
        {
            get => _slots;
            set
            {
                _slots = value;
                OnPropertyChanged();
            }
        }
        private ObservableCollection<int> _leftLineIC;
        public ObservableCollection<int> LeftLineIC
        {
            get => _leftLineIC;
            set
            {
                _leftLineIC = value;
                OnPropertyChanged();
            }
        }


        public LoadPortViewModel()
        {
            InitializeSlots();
        }

        private void InitializeSlots()
        {
            LeftLineIC = new ObservableCollection<int>();
            Slots = new ObservableCollection<SlotItem>();
            for (int i = 25; i >= 1; i--)
            {
                Slots.Add(new SlotItem
                {
                    Number = i,
                    Status = SlotStatus.Empty
                });
                LeftLineIC.Add(i);
            }
        }

        // 更新槽位状态的方法
        public void UpdateSlotStatus(int slotNumber, SlotStatus status)
        {
            var slot = Slots.FirstOrDefault(s => s.Number == slotNumber);
            if (slot != null)
            {
                slot.Status = status;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    public class HeightToFontSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double height)
            {
                return height * 0.03; // 3% of control height
            }
            return 12.0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class HeightToSlotFontSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double height)
            {
                return height * 0.02; // 2% of control height
            }
            return 12.0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class WidthToScaleLineConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double width)
            {
                return width * 0.8; // 80% of column width
            }
            return 10.0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}